package com.hyy.asm.protocol;

import com.hyy.asm.message.LoginRequestMessage;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

public class TestMessageCodec {
    public static void main(String[] args) throws Exception {
        EmbeddedChannel channel = new EmbeddedChannel(
                new LengthFieldBasedFrameDecoder(1024,12,4,0,0),
                new LoggingHandler(LogLevel.DEBUG),
                new MessageCodecSharable());
        LoginRequestMessage loginRequestMessage = new LoginRequestMessage();
        loginRequestMessage.setUsername("张三");
        loginRequestMessage.setPassword("123");
//        channel.writeOutbound(loginRequestMessage);

        ByteBuf out = ByteBufAllocator.DEFAULT.buffer();
        new MessageCodec().encode(null,loginRequestMessage,out);

        ByteBuf s1 = out.slice(0, 100);
        s1.retain();
        ByteBuf s2 = out.slice(100, out.readableBytes()-100);
//        s2.retain();
        channel.writeInbound(s1);
        channel.writeInbound(s2);
//        channel.writeInbound(s2);
    }
}
